package com.yehebl.orm.check;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.List;

import com.yehebl.orm.util.LogUtil;

public class EntityAuto {

	public static void write(String packageName, List<String> packList, boolean isTableId, String classDesc,
			String tableName, List<FieldAuto> fieldList,String fileUrl) {
		StringBuilder sb = new StringBuilder();
		sb.append("package ").append(packageName).append(";\r\n");
		sb.append("import com.yehebl.orm.anno.Entity;\r\n");
		sb.append("import com.yehebl.orm.anno.Column;\r\n");
		sb.append("import java.io.Serializable;\r\n");
		for (String p : packList) {
			sb.append("import ").append(p).append(";\r\n");
		}
		if (isTableId) {
			sb.append("import com.yehebl.orm.anno.Id;\r\n");
			sb.append("import com.yehebl.orm.enums.IdType;\r\n");
		}

		if (classDesc != null) {
			sb.append("/**\r\n");
			sb.append(" *").append(classDesc).append("\r\n");
			sb.append("*/\r\n");
		}
		sb.append("@Entity(\"").append(tableName).append("\")\r\n");
		sb.append("public class ").append(getClassName(tableName)).append(" implements Serializable{\r\n");
		sb.append("\tprivate static final long serialVersionUID=-1;\r\n");
		for (FieldAuto fa : fieldList) {
			sb.append("\t/**\r\n");
			sb.append("\t *").append(fa.getDesc()).append("\r\n");
			sb.append("\t*/\r\n");
			if (fa.isId()) {
				if(fa.getType().equals("String")){
					sb.append("\t@Id(IdType.uuid)\r\n");
				}else{
					sb.append("\t@Id(IdType.none)\r\n");
				}
			}
			sb.append("\t@Column\r\n");
			sb.append("\tprivate ").append(fa.getType()).append(" ").append(fa.getFieldName()).append(";\r\n");
		}

		for (FieldAuto fa : fieldList) {
			sb.append("\t/**\r\n");
			sb.append("\t *").append(fa.getDesc()).append("\r\n");
			sb.append("\t*/\r\n");
			sb.append("\tpublic ").append(fa.getType()).append(" get").append(getUpperFieldName(fa.getFieldName())).append("(){\r\n");
			sb.append("\t\treturn this.").append(fa.getFieldName()).append(";\r\n");
			sb.append("\t}\r\n");
			
			sb.append("\t/**\r\n");
			sb.append("\t *").append(fa.getDesc()).append("\r\n");
			sb.append("\t*/\r\n");
			sb.append("\tpublic void set").append(getUpperFieldName(fa.getFieldName())).append("(").append(fa.getType()).append(" ").append(fa.getFieldName()).append("){\r\n");
			sb.append("\t\tthis.").append(fa.getFieldName()).append("=").append(fa.getFieldName()).append(";\r\n");
			sb.append("\t}\r\n");
		}

		sb.append("}");
		
		File f=new File(fileUrl+File.separator+getClassName(tableName)+".java");
		if(f.exists()) {
			throw new RuntimeException("创建实体类出错同位置存在相同java文件,但是该java文件不是实体类:"+f.getPath());
		}
		if(!f.getParentFile().exists()) {
			if(!f.getParentFile().mkdirs()){
				throw new RuntimeException("文件夹创建失败:"+f.getPath());
			}
		}
		PrintStream ps;
		try {
			ps = new PrintStream(new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			LogUtil.printWarn("创建实体类出错："+e.getMessage());
			throw new RuntimeException(e);
		}  
		ps.print(sb);
		ps.close();
	}

	private static String getUpperFieldName(String fieldName) {
		return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
	}
	
	private static String getClassName(String tableName) {
		String [] t =tableName.split("_");
		StringBuilder sb=new StringBuilder();
		for (String s : t) {
			sb.append(s.substring(0, 1).toUpperCase());
			sb.append(s.substring(1).toLowerCase());
		}
		return sb.toString();
	}
	
}
